package com.tjbank.cssys.web.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.Random;
import java.util.concurrent.Executor;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
@Configuration
public class TaskExecutorConfig {
    @Bean("mytaskThreadExecutor")
    public Executor taskExecutorPool()
    {
        ThreadPoolTaskExecutor taskExecutor =new ThreadPoolTaskExecutor();
        //核心线程数
        taskExecutor.setCorePoolSize(1);
        //最大线程数
        taskExecutor.setMaxPoolSize(4);
        //线程空闲时间
        taskExecutor.setKeepAliveSeconds(1000);
        //线程队列数
        taskExecutor.setQueueCapacity(500);
        //拒绝策略
        taskExecutor.setRejectedExecutionHandler(new RejectedExecutionHandler() {
            @Override
            public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
                new Thread(r,"新线程"+new Random().nextInt(10)).start();
            }
        });
        return taskExecutor;
    }
}
